﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace STT.Admin.UserControls
{
    public partial class ucLop : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadLop();
            Loandd();
            btSua.Visible = false;
            btThemMoi.Visible = true;
        }

        void LoadLop()
        {
            gvLop.AutoGenerateColumns = false;
            gvLop.DataSource = BTbLop.SelectAll();
            gvLop.DataBind();
        }
        void Loandd()
        {
            ddlTenKH.DataSource = BTbKhoaHoc.SelectAll();
            ddlTenKH.DataTextField = "TenKhoaHoc";
            ddlTenKH.DataValueField = "MaKhoaHoc";
            ddlTenKH.DataBind();
        }
        bool KiemTraDuLieu()
        {
            return BTbLop.KiemTraDuLieu(new ETbLop
            {
              TenLop=txtTenLop.Text
            });
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvLop_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvLop.EditIndex = e.NewEditIndex;
            gvLop.DataBind();
        }

        protected void gvLop_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int MaLop = int.Parse(gvLop.DataKeys[e.RowIndex].Value.ToString());

                BTbLop.Delete(MaLop);
                LoadLop();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvLop_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvLop.EditIndex = -1;
            gvLop.DataBind();
        }

        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            if (!KiemTraDuLieu())
            {
                BTbLop.Insert(new ETbLop
                {
                  TenLop=txtTenLop.Text,
                  MaKhoaHoc=Int32.Parse( ddlTenKH.Text),
                  Siso=Int32.Parse(txtSiSo.Text)
                });

                LoadLop();
            }
            else
            {
                ThongBao("Khóa học này đã tồn tại!");

            }
        }
        protected void btImport_Click(object sender, EventArgs e)
        {

        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvLop.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvLop.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbLop.Delete(int.Parse(gvLop.DataKeys[i].Value.ToString()));
                    }
                }

                LoadLop();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        // Phân trang
        protected void gvLop_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvLop.PageIndex = e.NewPageIndex;
            gvLop.DataBind();
        }

        protected void gvLop_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                
                // Thay đổi tiêu đề
                txtTieuDe.InnerText = "Sửa khóa học";
                txtTenLop.Text = gvLop.Rows[index].Cells[2].Text;
                ddlTenKH.Text = gvLop.Rows[index].Cells[3].Text;
                txtSiSo.Text = gvLop.Rows[index].Cells[4].Text;
               
               
                btSua.Visible = true;
                btThemMoi.Visible = false;
                chonSuaClick = true;
            }
        }

        protected void btSua_Click(object sender, EventArgs e)
        {
            BTbLop.Update(new ETbLop
            {
              
               TenLop=txtTenLop.Text,
                MaKhoaHoc=int.Parse(ddlTenKH.SelectedValue),
                Siso=Int32.Parse(txtSiSo.Text)

               
               
            });
            LoadLop();
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            BTbLop.SearchTenLop(new ETbLop
            {
                TenLop= txtTimKiem.Text
            });
            LoadLop();
        }

       
     
    }
}